package com.atguigu.schedule.filter;

import com.atguigu.schedule.pojo.SysUser;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/*
 * 作用：拦截器
 * 作用域：web容器
 * 作用：拦截请求
 * 作用：拦截器
 * 作用：拦截请求
 *
 */
@WebFilter(urlPatterns = {"/showSchedule.html","/schedule/*"})
//@WebFilter(urlPatterns = {"/showSchedule.html","/schedule/*"},servletNames = {"scheduleServlet"})
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        //参数父转子
        HttpServletRequest request =(HttpServletRequest) servletRequest;
        HttpServletResponse response =(HttpServletResponse) servletResponse;

        //获取session域对象



        //从session域对象中获取用户对象
        HttpSession session = request.getSession();
        Object sysUser =(SysUser) session.getAttribute("sysUser");

        //判断用户对象是否为空
        if (sysUser == null) {
            //没登陆，到login.html
            response.sendRedirect(request.getContextPath()+"/login.html");
        }else {
            //登录过，放行
            filterChain.doFilter(servletRequest, servletResponse);
        }


        //没登陆，到login.html


        //登录过，放行




    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
